package com.shycloud.mido.music.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shycloud.mido.common.security.util.SecurityUtils;
import com.shycloud.mido.music.domain.MisMyCollectionMusic;
import com.shycloud.mido.music.domain.MisUserMusic;
import com.shycloud.mido.music.mapper.MisMyCollectionMapper;
import com.shycloud.mido.music.mapper.MisMyConllectionMusicMapper;
import com.shycloud.mido.music.mapper.MisUserCollectionMapper;
import com.shycloud.mido.music.mapper.MisUserMusicMapper;
import com.shycloud.mido.music.service.MisMyCollectionMusicService;
import com.shycloud.mido.music.util.CommonString;
import com.shycloud.mido.music.vo.AddMusicVO;
import com.shycloud.mido.music.vo.MisMusicVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author nianhua.jiang
 */
@Service
@AllArgsConstructor
public class MisMyCollectionMusicServiceImpl extends ServiceImpl<MisMyConllectionMusicMapper, MisMyCollectionMusic> implements MisMyCollectionMusicService {

	/** 自建曲集与曲目关系表 */
	private MisMyConllectionMusicMapper mapper;
	/** 单曲收藏模块 mapper */
	private MisUserMusicMapper userMusicMapper;
	/** 自建曲集模块 mapper */
	private MisMyCollectionMapper myCollectionMapper;
	/** 收藏曲集模块 mapper */
	private MisUserCollectionMapper userCollectionMapper;

	/**
	 * 获取自建曲集下的曲目列表
	 *
	 * @param page
	 * @param myCollectionId
	 * @return com.baomidou.mybatisplus.core.metadata.IPage<com.shycloud.mido.music.vo.MisMusicVO>
	 * @author nianhua.jiang
	 * @date 2020/6/18 11:14
	 */
	@Override
	public IPage<MisMusicVO> getMyMusic(Page page, Integer myCollectionId) {
		return mapper.getMyMusic(page, myCollectionId, SecurityUtils.getMember().getId());
	}


	/**
	 * 自建曲集添加曲目列表
	 *
	 * @return java.util.List<com.shycloud.mido.music.vo.AddMusicVO>
	 * @author nianhua.jiang
	 * @date 2020/9/1 13:24
	 **/
	@Override
	public List<AddMusicVO> getAddMusicList() {

		List<AddMusicVO> resultList = new ArrayList<>();

		//单曲收藏
		AddMusicVO addMusicVO = new AddMusicVO();
		addMusicVO.setName("单曲收藏");
		addMusicVO.setCollectionId(-1);
		addMusicVO.setMyCollectionId(-1);
		//获取单曲收藏的曲目数量
		addMusicVO.setMusicCount(userMusicMapper.selectCount(new QueryWrapper<MisUserMusic>()
				.eq(MisUserMusic.COL_MEMBER_ID, SecurityUtils.getMember().getId())
				.and(x -> x.eq(MisUserMusic.COL_DEL_FLAG, CommonString.DELETE_FLG))
				.and(y -> y.eq(MisUserMusic.COL_TYPE, CommonString.USER_MUSIC_TYPE_SAVE))));

		resultList.add(addMusicVO);

		//自建曲集
		List<AddMusicVO> myCollectionList = myCollectionMapper.getMyCollectionForAddMusicPage(SecurityUtils.getMember().getId());
		resultList.addAll(myCollectionList);

		//收藏曲集
		List<AddMusicVO> saveCollectionList = userCollectionMapper.getUserCollectionForAddMusicPage(SecurityUtils.getMember().getId());
		resultList.addAll(saveCollectionList);

		return resultList;
	}
}
